﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleDBase
{
    class DBase
    {
    
        public string Name { get; set; }
        public Table activetable { get; private set; }


        public DBase(string dbname)
        {
            Name = dbname;
            activetable = null;
        }

        public void AddEmptyTable(string tablename)
        {
            /*activetable = new Table();
            activetable.Name = tablename;*/

            string path = "./datasource/" + Name + "." + tablename + ".dat";
            using (StreamWriter sw = File.CreateText(path))
            {
                sw.WriteLine(tablename);
                sw.WriteLine(0);
            }
        }

        public void ActivateTable(string tablename)
        {
            activetable = new Table("./datasource/" + Name + "." + tablename + ".dat");
        }

        public void DeactivateTable()
        {
            activetable = null;
        }

        public void DeleteActiveTable()
        {
            string path = "./datasource/" + Name + "." + activetable.Name + ".dat";

            FileInfo fi = new FileInfo(path);
            fi.Delete();

            fi = null;
            activetable = null;
        }

        public void PrintActiveTable(int cursorleft, int cursortop)
        {
            activetable.PrintTable(cursorleft, cursortop);
        }

        public void SaveActiveTable()
        {
            string path = "./datasource/" + Name + "." + activetable.Name + ".dat";
            
            FileInfo fi = new FileInfo(path);
            fi.Delete();
            fi = null;

            using (StreamWriter sw = File.CreateText("./datasource/" + Name + "." + activetable.Name + ".dat"))
            {
                sw.WriteLine(activetable.Name);
                
                sw.WriteLine(activetable.TableFields.Count);
                for (int i = 0; i < activetable.TableFields.Count; ++i)
                {
                    sw.Write(activetable.TableFields[i].Name + "\t");
                    sw.Write(activetable.TableFields[i].Type + "\t");

                    bool isnullable = activetable.TableFields[i].IsNullable;
                    if (isnullable)
                    {
                        sw.WriteLine("nullable");
                    }
                    else
                    {
                        sw.WriteLine("notnullable");
                    }
                }

                for (int i = 0; i < activetable.NumOfRecords; ++i)
                {
                    for (int j = 0; j < activetable.TableFields.Count; ++j)
                    {
                        if (activetable.Records[i][j].IsNull)
                        {
                            sw.Write("null");
                        }
                        else
                        {
                            sw.Write(activetable.Records[i][j].GetValue());
                        }
                        if (j != activetable.TableFields.Count - 1) sw.Write("\t");
                    }

                    sw.WriteLine();
                }

            }
        }
    }
}
